<?php
declare (strict_types=1);

namespace app\admin\model\iot;

use app\BaseModel;

/**
 * 设备通信日志表
 */
class DeviceLog extends BaseModel
{

    /**
     * 获取设备日志列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        foreach ($data['data'] as &$v) {
            $v['msg'] = json_decode($v['msg'], true);
        }
        return $data;
    }

    /**
     * 写入设备通信日志
     * @param int $Cmd
     * @param string $Imei
     * @param string $ReqId
     * @param string $msg
     * @param string $mode
     * @param string $result
     * @param int $msg_time
     * @param string $remark
     * @return DeviceLog|\think\Model
     */
    public static function writeDeviceLog(int $Cmd, string $Imei, string $ReqId, string $msg, string $mode, string $result, int $msg_time, string $remark = ''): DeviceLog|\think\Model
    {
        $data = [
            'cmd' => $Cmd,
            'imei' => $Imei,
            'req_id' => $ReqId,
            'msg' => $msg,
            'mode' => $mode,
            'result' => $result,
            'remark' => $remark,
            'msg_time' => $msg_time,
            'create_time' => time(),
        ];
        return self::create($data);
    }

}




